<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <el-icon class="icon" size="20" @click="store.commit('collapseMenu')">
        <Fold />
      </el-icon>

      <!-- tab区域 -->
      <ul class="flex-box">
        <li v-for="(item,index) in selectMenu" :key="item.path" class="tab flex-box"
          :class="{selected:route.path===item.path}">
          <el-icon size="18">
            <component :is="item.icon"></component>
          </el-icon>
          <router-link class="flex-box" :to="{path:item.path}">{{item.name}}</router-link>
          <el-icon size="12" class="close" @click="closeTab(item,index)">
            <Close />
          </el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
      <!-- @command="handleClick"：这是一个事件监听器   当用户点击下拉菜单中的选项时，会触发 handleClick 方法，并传递被点击项的 command 属性值 -->
      <el-dropdown @command="handleClick">
        <span class="el-dropdown-link flex-box">
          <el-avatar :src="userInfo.avatar" />
          <p class="user-name">{{userInfo.name}}</p>
        </span>
        <!-- 退出登录 -->
        <template #dropdown>
          <el-dropdown-item command="cancel">退出</el-dropdown-item>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>


<script setup>
  import { useStore } from 'vuex'
  import { computed } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  const store = useStore()
  // 当前的路由对象
  const route = useRoute()
  const router = useRouter()
  const selectMenu = computed(() => store.state.menu.selectMenu)
  // 关闭tag方法
  const closeTab = (item, index) => {
    store.commit('closeMenu', item)
    // 删除的不是当前页的tag
    if (route.path !== item.path) {
      return
    }
    const selectMenuData = selectMenu.value
    // console.log(selectMenuData);
    //  删除的是最后一项
    if (index === selectMenuData.length) {
      // 如果tag只有一个元素
      if (!selectMenuData.length) {
        router.push('/')
      } else {
        router.push({
          path: selectMenuData[index - 1].path
        })
      }
    } else {
      // 如果删除的是中间位置tag
      router.push({
        path: selectMenuData[index].path
      })
    }
  }

  const userInfo = JSON.parse(localStorage.getItem('pz_userInfo'))
  // 退出登录
  const handleClick = (command) => {
    if (command === 'cancel') {
      localStorage.removeItem('pz_token')
      localStorage.removeItem('pz_userInfo')
      localStorage.removeItem('pz_v3pz')
      window.location.href = window.location.origin
    }
  }
</script>

<style lang="less" scoped>
  .flex-box {
    display: flex;
    align-items: center;
    height: 100%;
  }

  .header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    background-color: #fff;
    padding-right: 25px;

    .header-left {
      .icon {
        width: 45px;
        height: 100%
      }

      .icon:hover {
        background-color: #f5f5f5;
        cursor: pointer
      }

      .tab {
        padding: 0 10px;
        height: 100%;

        .text {
          margin: 0 5px;
        }

        .close {
          visibility: hidden;
        }

        &.selected {
          a {
            color: #409eff;
          }

          i {
            color: #409eff;
          }

          background-color: #f5f5f5;
        }
      }

      .tab:hover {
        background-color: #f5f5f5;

        .close {
          visibility: inherit;
          cursor: pointer;
          color: #000;
        }
      }
    }

    .header-right {
      .user-name {
        margin-left: 10px;
      }
    }

    a {
      height: 100%;
      color: #333;
      font-size: 15px;
    }
  }
</style>